package com.kongge.viewdisplayframeworklib.model.queue;

import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;

import com.kongge.viewdisplayframeworklib.R;
import com.kongge.viewdisplayframeworklib.event.param.EQParam;
import com.kongge.viewdisplayframeworklib.model.AbsUIController;
import com.kongge.viewdisplayframeworklib.model.NavigateItem;
import com.kongge.viewdisplayframeworklib.model.PageNode;

import java.util.LinkedList;
import java.util.List;

/**
 * author:kongge
 * date:2018/9/11
 * layout:
 * description:
 */

public class PageQueue extends AbsUIController {

    private ITabbar tabbar;

    private IScoller scrollerView;

    private PageQueueNode pageQueueNode;

    private List<AbsUIController> uiControllerList;

    private AbsUIController currentUIController;


    public PageQueue(PageNode pageNode, ViewGroup viewGroup) {
        super(pageNode, viewGroup);
        pageQueueNode = (PageQueueNode) pageNode;

        tabbar = (ITabbar) viewGroup.findViewById(R.id.pagequeue_tabbar);
        scrollerView = (IScoller) viewGroup.findViewById(R.id.pagequeue_scrollview);

        initTabbar();
    }

    @Override
    public void dispatchParam(EQParam param) {
        if (param == null || uiControllerList == null) {
            return;
        }
        List<Integer> targetReceivePageIdList = param.getTargetReceivePageIdList();
        for (AbsUIController uiController : uiControllerList) {
            int pageId = uiController.getPageId();
            if (targetReceivePageIdList != null) {
                for (Integer targetPageId : targetReceivePageIdList) {
                    if (targetPageId == pageId) {
                        uiController.dispatchParam(param);
                        break;
                    }
                }
            } else {
                uiController.dispatchParam(param);
            }
        }
    }

    @Override
    public void dispatchEvent(int event) {

    }

    @Override
    public boolean isBottomBarVisible() {
        if (currentUIController != null) {
            return currentUIController.isBottomBarVisible();
        }
        return true;
    }

    @Override
    public NavigateItem getNavigateItem() {
        if (currentUIController != null) {
            return currentUIController.getNavigateItem();
        }
        return null;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (currentUIController != null) {
            return currentUIController.onKeyDown(keyCode, event);
        }
        return false;
    }

    @Override
    public void broadcastScreenChange() {
        if (currentUIController != null) {
            currentUIController.broadcastScreenChange();
        }
    }

    public void initUIControllers(final List<AbsUIController> uiControllers, int defaultFocusFrameId) {
        if (uiControllers == null) {
            return;
        }
        uiControllerList = uiControllers;
        List<View> views = new LinkedList<>();
        int selectIndex = 0;
        for (int i = 0; i < uiControllers.size(); i++) {
            AbsUIController uiController = uiControllers.get(i);
            View view = uiController.getRootView();
            views.add(view);
            if (uiController.getPageId() == defaultFocusFrameId) {
                selectIndex = i;
                currentUIController = uiController;
            }
        }

        scrollerView.setScrollerViews(views, selectIndex);
        scrollerView.setPageSelectChangeListener(new OnPageSelectChangeListener() {
            @Override
            public void onPageSelectChange(View view, int oldIndex, int curIndex) {
                AbsUIController oldUIController = currentUIController;
                if (oldUIController != null) {
                    oldUIController.onBackground();
                }
                currentUIController = uiControllerList.get(curIndex);
                if (currentUIController != null) {
                    currentUIController.onForeground();
                }
                tabbar.setSelectIndex(curIndex);
            }
        });

    }

    private void initTabbar() {

        List<PageNode> subPageNodeList = pageQueueNode.getSubPageNodeList();
        tabbar.setTabList(subPageNodeList);
        tabbar.setPageSelectChangeListener(new OnPageSelectChangeListener() {
            @Override
            public void onPageSelectChange(View view, int oldIndex, int curIndex) {
                if (scrollerView != null) {
                    scrollerView.setSelectIndex(curIndex);
                }
            }
        });
    }

    @Override
    public void onForeground() {
        super.onForeground();
        if (currentUIController != null) {
            currentUIController.onForeground();
        }
    }

    @Override
    public void onBackground() {
        super.onBackground();
        if (currentUIController != null) {
            currentUIController.onBackground();
        }
    }

    @Override
    public void onRemove() {
        super.onRemove();
        if (uiControllerList != null) {
            for (AbsUIController uiController : uiControllerList) {
                uiController.onRemove();
            }
        }
    }
}
